package com.yxw.live_vod_backend_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_backend_boot.dto.NoticeTypeUpdateDto;
import com.yxw.live_vod_backend_boot.entity.YxNoticeTypeEntity;
import com.yxw.live_vod_backend_boot.mapper.NoticeTypeMapper;
import com.yxw.live_vod_backend_boot.service.NoticeTypeService;
import com.yxw.live_vod_backend_boot.util.PageUtils;
import com.yxw.live_vod_backend_boot.util.UserUtil;
import com.yxw.live_vod_backend_facade.dto.NoticeTypeQueryDto;
import com.yxw.live_vod_backend_facade.vo.NoticeTypeVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuxin
 * @create 2023-12-06
 * @description 系统消息通知类型管理
 */
@Service
public class NoticeTypeServiceImpl extends ServiceImpl<NoticeTypeMapper, YxNoticeTypeEntity> implements NoticeTypeService {

    @Autowired
    private UserUtil userUtil;
    /**
     * 查询消息通知类型列表
     * @param dto
     * @return
     */
    @Override
    public PageVO<NoticeTypeVo> pageNoticeTypeList(NoticeTypeQueryDto dto) {
        List<NoticeTypeVo> voList=new ArrayList<>();
        Page<YxNoticeTypeEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        LambdaQueryWrapper<YxNoticeTypeEntity> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(dto.getNoticeTypeCode()),YxNoticeTypeEntity::getNoticeTypeCode,dto.getNoticeTypeCode());
        queryWrapper.eq(StringUtils.isNotBlank(dto.getUseTypeCode()),YxNoticeTypeEntity::getUseTypeCode,dto.getUseTypeCode());
        queryWrapper.eq(null!=dto.getStatus(),YxNoticeTypeEntity::getStatus,dto.getStatus());
        Page<YxNoticeTypeEntity> entityPage = baseMapper.selectPage(page, queryWrapper);
        List<YxNoticeTypeEntity> records = entityPage.getRecords();
        if(null != records && !records.isEmpty()){
            voList= BeanUtil.copyToList(records, NoticeTypeVo.class);
        }
        return PageUtils.get(page,voList);
    }

    /**
     * 更新通知类型内容和状态
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNoticeTypeById(NoticeTypeUpdateDto dto) {
        LambdaUpdateWrapper<YxNoticeTypeEntity> updateWapper=new LambdaUpdateWrapper();
        updateWapper.set(StringUtils.isNotBlank(dto.getContent()),YxNoticeTypeEntity::getContent,dto.getContent());
        updateWapper.set(null!=dto.getStatus(),YxNoticeTypeEntity::getStatus,dto.getStatus());
        updateWapper.set(YxNoticeTypeEntity::getUpdateTime,new Date());
        updateWapper.set(YxNoticeTypeEntity::getUpdateId, userUtil.getLoginUserId());
        updateWapper.set(YxNoticeTypeEntity::getUpdateName, userUtil.getLoginName());
        updateWapper.eq(YxNoticeTypeEntity::getTypeId,dto.getTypeId());
        int cnt = baseMapper.update(null, updateWapper);
        if(0==cnt){
            throw new BaseException("未找到消息类型数据进行更新");
        }
    }
}
